package com.li.service;

import com.common.util.IdWorker;
import com.li.dao.UserFileMapper;
import com.li.dao.UserMapper;
import com.li.model.TableData;
import com.li.model.User;
import com.li.model.UserFile;
import com.common.util.MyFileUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserFileServiceImpl implements UserFileService{
    @Autowired
    private UserFileMapper userFileMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IdWorker idWorker;
    @Override
    public int addFile(UserFile file) {
        file.setId(idWorker.nextId());
        return userFileMapper.addFile(file);
    }

    @Override
    public void deleteFileById(long userId) {
        userFileMapper.deleteFileById(userId);
    }

    @Override
    public List<UserFile> queryFileByUserId(long userId) {
       return userFileMapper.queryFileByUserId(userId);
    }

    @Override
    public UserFile queryFileByName(Map<String, Object> map) {
        return userFileMapper.queryFileByName(map);
    }

    @Override
    public void deleteFileByFileName(Map<String, Object> map) {
        userFileMapper.deleteFileByFileName(map);
    }
    @Override
    public void updateFile(UserFile userFile) {
        userFileMapper.updateFile(userFile);
    }

    @Override
    public List<UserFile> queryAllFile() {
        return userFileMapper.queryAllFile();
    }

    @Override
    public UserFile queryFileById(long id) {
        return userFileMapper.queryFileById(id);
    }

    public String upload(CommonsMultipartFile file,HttpServletRequest request){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();

        User user =((User)session.getAttribute("USER_SESSION"));
        String fileName = file.getOriginalFilename();
        //文件；类型检查
        String rex = fileName.substring(fileName.lastIndexOf(".")+1);
        if (!("xlsx".equals(rex)||"xls".equals(rex))){
            System.out.println(rex);
            return "请上传xlsx格式或者xls格式的Excel文件";
        }

        System.out.println(user.getUsername());
        String savePath ="/userFile/upload/"+user.getUsername();
        UserFile userFile = new UserFile(idWorker.nextId(),request.getServletContext().getRealPath(savePath),fileName,user.getId(),new Date(),0);

        //查询是否重复
        Map<String,Object> map = new HashMap<>();
        map.put("userId",user.getId());
        map.put("fileName",fileName);
        if (userFileMapper.queryFileByName(map) != null){
            return "文件："+file.getOriginalFilename()+"上传重复";
        }
        //保存文件信息到数据库
        if (userFileMapper.addFile(userFile)==0){
            return "文件："+file.getOriginalFilename()+"上传失败";
        }
        //物理保存文件
        if(MyFileUtil.fileUpload(file,request,savePath)){
            return "文件："+file.getOriginalFilename()+"成功上传";
        }else {
            userFileMapper.deleteFileByFileName(map);
            return "文件："+file.getOriginalFilename()+"上传失败";
        }
    }

    public TableData fileToList(String fileName,long fid,HttpServletRequest request){
        TableData table = null;
        HttpSession session = request.getSession();
        UserFile file = userFileMapper.queryFileById(fid);

        try {
            table = MyFileUtil.getFileContent(file.getSavePath()+"/"+fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return table;
    }
}
